﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfStudy.Command
{
    public interface IView
    {
        //属性
        bool IsChanged { get; set; }
        //方法
        void SetBinding();
        void Refresh();
        void Clear();
        void Save();
    }

    public class ClearCommand : ICommand
    {
        //当命令执行状态改变时，应当被激发
        public event EventHandler CanExecuteChanged;

        //用于判断命令是否可以执行
        public bool CanExecute(object parameter)
        {
            return true;
        }

        //命令执行，带有与业务相关的Clear类
        public void Execute(object parameter)
        {
            IView view = parameter as IView;
            if (view!=null)
            {
                view.Clear();
            }
        }
    }

    public class MyCommandSource : UserControl, ICommandSource
    {
        public ICommand Command { get; set; }

        public object CommandParameter { get; set; }

        public IInputElement CommandTarget { get; set; }

        public MyCommandSource()
        {
            this.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.ClearButtonClicked));
        }

        private void ClearButtonClicked(object sender, RoutedEventArgs e)
        {
            if (CommandTarget != null)
            {
                Command.Execute(CommandTarget);
            }
        }

        /*protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            if (CommandTarget != null)
            {
                Command.Execute(CommandTarget);
            }
        }*/
    }
}
